package com.lanling.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lanling.eduservice.entity.vo.CourseInfoVo;
import com.lanling.eduservice.entity.vo.CoursePublishVo;
import com.lanling.eduservice.entity.vo.CourseQuery;
import com.lanling.eduservice.entity.EduCourse;
import com.lanling.eduservice.orther.R;
import com.lanling.eduservice.service.EduCourseService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author 胡振华
 * @since 2020-11-10
 */
@RestController
@RequestMapping("/eduservice/edu-course")
@CrossOrigin
public class EduCourseController {


    @Autowired
    private EduCourseService courseService;


    //添加课程基本信息的方法
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        //返回添加之后课程id，为了后面添加大纲使用
        String id = courseService.saveCourseInfo(courseInfoVo);
            return R.ok().data("courseId",id);
    }

    //根据课程id查询课程基本信息
    @GetMapping("getCourseInfo/{couseId}")
    public R getCourseInfo(@PathVariable String couseId){
        CourseInfoVo courseInfoVo = courseService.getCourseInfo(couseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }

    //更改课程信息
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    //根据课程id查询课程确认信息
    @GetMapping("getCourseInfoById/{courseId}")
    public R getCourseInfoById(@PathVariable String courseId){
        CoursePublishVo coursePublishVo = courseService.getCourseInfoById(courseId);
        return R.ok().data("coursePublishVo",coursePublishVo);
    }

    //课程最终发布
    @PostMapping("publishCourse/{courseId}")
    public R publishCourse(@PathVariable String courseId){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(courseId);
        eduCourse.setStatus("Normal");
        courseService.updateById(eduCourse);
        return R.ok();
    }


    //查询课程列表
    //多条件这查询，带分页
    @ApiOperation(value = "多条件组合分页查询")
    @PostMapping("getListCourse/{page}/{limit}")
    public R getListCourse(@PathVariable long page, @PathVariable long limit, @RequestBody(required = false) CourseQuery courseQuery){
        Page<EduCourse> eduCoursePage = new Page<>(page,limit);
        System.out.println(page+"------------"+limit);
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper();
        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();
        String begin = courseQuery.getBegin();
        String end = courseQuery.getEnd();
        if (!StringUtils.isEmpty(title)){
            queryWrapper.like("title",title);
        }
        if (!StringUtils.isEmpty(status)){
            queryWrapper.eq("status",status);
        }
        if (!StringUtils.isEmpty(begin)){
            queryWrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end)){
            queryWrapper.le("gmt_create",end);
        }
        //排序
        queryWrapper.orderByDesc("gmt_create");
        courseService.page(eduCoursePage,queryWrapper);
        long total = eduCoursePage.getTotal();
        List<EduCourse> records = eduCoursePage.getRecords();
        for (EduCourse record : records) {
            System.out.println(record.getTitle());
        }
        return R.ok().data("total",total).data("records",records);
    }

    //删除课程
    @GetMapping("removeCourse/{courseId}")
    public R removeCourse(@PathVariable String courseId){
        courseService.removeCourse(courseId);
        return R.ok();
    }


}